package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.dto.AttendQuoteDataBaseDTO;
import com.engine.salary.entity.datacollection.param.*;
import com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.AttendQuoteDataService;
import com.engine.salary.service.AttendQuoteFieldService;
import com.engine.salary.service.impl.AttendQuoteDataServiceImpl;
import com.engine.salary.service.impl.AttendQuoteFieldServiceImpl;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.Column;
import com.engine.salary.util.page.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 考勤数据
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class AttendQuoteDataWrapper extends Service {

    private AttendQuoteDataService getAttendQuoteDataService(User user) {
        return ServiceUtil.getService(AttendQuoteDataServiceImpl.class, user);
    }

    private AttendQuoteFieldService getAttendQuoteFieldService(User user) {
        return ServiceUtil.getService(AttendQuoteFieldServiceImpl.class, user);
    }

    /**
     * 同步考勤引用数据
     *
     * @param syncParam
     * @return
     */
    public String syncAttendQuoteData(AttendQuoteDataSyncParam syncParam) {
        return getAttendQuoteDataService(user).syncAttendQuoteData(syncParam);
    }

    /**
     * 查看考勤引用数据
     *
     * @param queryParam
     * @return
     */
    public Map<String, Object> view(AttendQuoteDataQueryParam queryParam) {
        Long id = queryParam.getAttendQuoteId();
        if (id == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100253, "考勤引用id不能为空"));
        }
        // 考勤数据分页主数据
        PageInfo<AttendQuoteDataBaseDTO> page = getAttendQuoteDataService(user).listPage(queryParam);
        List<AttendQuoteDataBaseDTO> attendQuoteDataBases = page.getList();
        // 所有考勤字段
        List<AttendQuoteFieldPO> attendQuoteFields = getAttendQuoteFieldService(user).getAllAttendQuoteFields();
        // 获取最终结果
        List<Map<String, Object>> listMaps = getAttendQuoteDataService(user).getListMaps(attendQuoteDataBases);

        //当前引用的值
        List<String> effectiveColumns = listMaps.stream()
                .map(Map::keySet)
                .max(Comparator.comparingInt(Set::size))
                .orElse(new HashSet<>())
                .stream()
                .map(key -> key.split("_")[0])
                .collect(Collectors.toList());


        PageInfo<Map<String, Object>> listPage = new PageInfo<>(listMaps);
        listPage.setPageNum(page.getPageNum());
        listPage.setPageSize(page.getPageSize());
        listPage.setTotal(page.getTotal());
        List<Column> columns = new ArrayList<>();
        columns.add(Column.builder().title("姓名").dataIndex("username").key("username").display(Boolean.TRUE).build());
        columns.add(Column.builder().title("部门").dataIndex("departmentName").key("departmentName").display(Boolean.TRUE).build());
        columns.add(Column.builder().title("手机号").dataIndex("mobile").key("mobile").display(Boolean.TRUE).build());
        columns.add(Column.builder().title("工号").dataIndex("jobNum").key("jobNum").display(Boolean.TRUE).build());
        columns.add(Column.builder().title("证件号码").dataIndex("idNo").key("idNo").display(Boolean.TRUE).build());
        if (CollectionUtils.isNotEmpty(listMaps)) {
            attendQuoteFields.stream()
                    .filter(attendQuoteField->effectiveColumns.contains(Util.null2String(attendQuoteField.getId())))
                    .forEach(attendQuoteField -> {
                        columns.add(Column.builder().title(attendQuoteField.getFieldName())
                                .dataIndex(attendQuoteField.getId() + "_attendQuoteData")
                                .key(attendQuoteField.getId() + "_attendQuoteData").display(Boolean.TRUE).build());
                    }
            );

        }
        listPage.setColumns(columns);
        // 表格表头
//        List<WeaTableColumn> columns = new ArrayList<>();
//        columns.add(new WeaTableColumn("150", SalaryI18nUtil.getI18nLabel(85429, "姓名"), "username"));
//        columns.add(new WeaTableColumn("150", SalaryI18nUtil.getI18nLabel(86185, "部门"), "departmentName"));
//        columns.add(new WeaTableColumn("150", SalaryI18nUtil.getI18nLabel(86186, "手机号"), "mobile"));
//        columns.add(new WeaTableColumn("150", SalaryI18nUtil.getI18nLabel(86317, "工号"), "jobNum"));
        // 动态列
//        if (CollectionUtils.isNotEmpty(listMaps)) {
//            for (AttendQuoteFieldPO attendQuoteField : attendQuoteFields) {
//                columns.add(new WeaTableColumn("150", attendQuoteField.getFieldName(), attendQuoteField.getId() + "_attendQuoteData"));
//            }
//        }
//
//        WeaTable weaTable = new WeaTable();
//        weaTable.setColumns(columns);

//
//        WeaResultMsg result = new WeaResultMsg(false);
//        result.putAll(weaTable.makeDataResult());
//        result.success();

        Map<String, Object> datas = new HashMap<>();
        datas.put("pageInfo", listPage);
//        datas.put("dataKey", result.getResultMap());

        return datas;
    }

    /**
     * 下载导入模板
     *
     * @param templateParam
     * @return
     */
    public XSSFWorkbook downloadTemplate(AttendQuoteDataExportTemplateParam templateParam) {
        return getAttendQuoteDataService(user).downloadTemplate(templateParam);
    }

    /**
     * 导出考勤引用数据
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook export(AttendQuoteDataQueryParam queryParam) {
        return getAttendQuoteDataService(user).export(queryParam);
    }

    /**
     * 预览
     *
     * @param param
     * @return
     */
    public Map<String, Object> preview(AttendQuoteDataImportParam param) {
        return getAttendQuoteDataService(user).preview(param);
    }


    /**
     * 导入
     *
     * @param param
     * @return
     */
    public Map<String, Object> importAttendQuoteData(AttendQuoteDataImportParam param) {
        return getAttendQuoteDataService(user).importAttendQuoteData(param);
    }

    public void editData(AttendQuoteDataEditParam attendQuoteDataEditParam) {
        getAttendQuoteDataService(user).editData(attendQuoteDataEditParam);
    }

    /**
     * 获取数据
     */
    public Map<String, Object> getData(AttendQuoteDataEditParam attendQuoteDataEditParam) {
        return getAttendQuoteDataService(user).getData(attendQuoteDataEditParam);
    }
}
